package com.aba.hos.admin;

import com.aba.hos.doc.gui.HospitalManagementGUI;
import com.aba.hos.pat.patientExampleWindow;
import com.aba.hos.totality.LoginWindow;

import javax.swing.*;
import java.awt.*;
import java.sql.SQLException;

public class AdminLoginExampleWindow extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            new LoginWindow();
        });
    }

    public void openMainPage(String username,String role) {
        JFrame mainFrame = new JFrame("主页面 - " + username);
        mainFrame.setSize(800, 600);
        mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        mainFrame.setLocationRelativeTo(null); // 居中显示

        // 创建主面板
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(null);

        // 添加欢迎标签
        JLabel welcomeLabel = new JLabel("欢迎您管理员"+"，" + username + "!");
        welcomeLabel.setFont(new java.awt.Font("宋体", 1, 24));
        welcomeLabel.setBounds(300, 100, 300, 50);
        mainPanel.add(welcomeLabel);

        // 患者管理端
        JButton patientBtn = new JButton("患者管理");
        patientBtn.setBounds(100, 200, 150, 50);
        patientBtn.addActionListener(e -> {
            mainFrame.dispose(); // 关闭主页面
            SwingUtilities.invokeLater(() -> {
                try {
                    new patientExampleWindow().openMainPage(username, role);
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            });
        });
        mainPanel.add(patientBtn);

        // 医生管理端
        JButton doctorBtn = new JButton("医生管理");
        doctorBtn.setBounds(300, 200, 150, 50);

        doctorBtn.addActionListener(e -> {
            mainFrame.dispose(); // 关闭主页面
            //打开医生管理端
            SwingUtilities.invokeLater(() -> {
                HospitalManagementGUI gui = null;
                try {
                    gui = new HospitalManagementGUI(username,role);
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
                gui.setVisible(true);
            });
        });
        mainPanel.add(doctorBtn);

        JButton logoutBtn = new JButton("退出登录");
        logoutBtn.setBounds(600, 500, 150, 30);
        logoutBtn.addActionListener(e -> {
            int confirm = JOptionPane.showConfirmDialog(
                    mainFrame,
                    "确定要退出登录吗？",
                    "确认退出",
                    JOptionPane.YES_NO_OPTION
            );
            if (confirm == JOptionPane.YES_OPTION) {
                mainFrame.dispose();
                new LoginWindow();
            }
        });
        mainPanel.add(logoutBtn);

        mainFrame.add(mainPanel);
        mainFrame.setVisible(true);
    }


}